// src/api/address.ts
import apiClient from '@/api/axios.js'

export class AddressApi {
    // 获取用户地址列表
    static async getAddressList(userID: number) {
        try {
            const response = await apiClient.get(`/useraddr/${userID}`, {
                headers: {
                    Authorization: `Bearer ${localStorage.getItem('token')}`
                }
            })
            return response.data
        } catch (error) {
            throw new Error(error.message || '获取地址列表失败')
        }
    }

    // 新增地址
    static async addAddress(params: {
        userID: number
        address: string
        phone: string
        status?: boolean
    }) {
        try {
            const response = await apiClient.post('/useraddr', params, {
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Bearer ${localStorage.getItem('token')}`
                }
            })
            return response.data
        } catch (error) {
            throw new Error(error.message || '新增地址失败')
        }
    }

    // 更新地址
    static async updateAddress(params: {
        addrID: number
        userID: number
        address: string
        phone: string
        status?: boolean
    }) {
        try {
            const response = await apiClient.put('/useraddr', params, {
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Bearer ${localStorage.getItem('token')}`
                }
            })
            return response.data
        } catch (error) {
            throw new Error(error.message || '更新地址失败')
        }
    }

    // 删除地址
    static async deleteAddress(addrID: number) {
        try {
            const response = await apiClient.delete(`/useraddr/${addrID}`, {
                headers: {
                    Authorization: `Bearer ${localStorage.getItem('token')}`
                }
            })
            return response.data
        } catch (error) {
            throw new Error(error.message || '删除地址失败')
        }
    }
}